var app = getApp();
var https = "https://www.moreclub.cn/";
var http2 = 'http://59.110.53.149:8088/'
var showbox;
var istrue = false;
var isInto = true;

var add0 = function (m) {
  return m < 10 ? '0' + m : m
}
var time = function (prefeerTime) {
  var time = new Date(prefeerTime);
  // var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var h = time.getHours();
  var mm = time.getMinutes();
  // var s = time.getSeconds();
  // return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
  return add0(m) + '月' + add0(d) + '日' + ' ' + add0(h) + ':' + add0(mm)
}

Page({
  data:{
    showView:true,
    totallist:[]
  },

  onShow:function(){
    var that = this;
    var personArr = wx.getStorageSync('personArr') || []; 
    for (var i = 0; i < personArr.length; i++) {
      if (personArr[i].isShow) {
        that.setData({
          showbox: true
        })
      } else {
        that.setData({
          showbox: false
        })
      }
    }

    var falg = false
    wx.getStorage({
      key: 'chatMsg1',
      success: function (res) {
        console.log(res.data)

        if (res.data.data && !falg) {
          that.setData({
            showbox: true
          })

        }
      }
    })
    falg = true
  },

  onLoad:function(options){
    var that = this;
    var myUsername = wx.getStorageSync('myUsername');
    var thumb = wx.getStorageSync('thumb');
    var uid = wx.getStorageSync('uid');
    var access_token = wx.getStorageSync('inToken');
   
    var w = options.width;
    that.setData({
      myUsername: myUsername,
      thumb: thumb
    })

    

    wx.request({
      url: https + 'v2/ugc/open/lists?uid=' + uid + '&page=0&pageSize=20&friendUid=' + uid +'&type=0',
      success: function (res) {
        console.log(res)
        if (res.data.success) {
          var pubData = res.data.data
          // try {
          //   wx.setStorageSync('pubData', pubData);
          // } catch (e) {
          // }

          var pubList = [];//创建一个新数组
          for (var key in pubData) {
            var thumb = pubData[key].thumb;//头像
           
            var photos = pubData[key].pictures;//图片
             

            // var prefeerContent = pubData[key].prefeerContent;//内容
            // var contentArr = prefeerContent.split('\n');
            var content = pubData[key].content;
            var nickName = pubData[key].nickName;
            if (pubData[key].likeDto.userLikes) {
              var userLikes = pubData[key].likeDto.userLikes.reverse();//点赞人显示
            } else {
              var userLikes = pubData[key].likeDto.userLikes
            }

            var createTime = pubData[key].createTime;//时间
            var pid = pubData[key].pid;
            var isClick = pubData[key].likeDto.clicked;
            var likeTimes = pubData[key].likeDto.likeTimes;
            var merchantName = pubData[key].merchantName;
            var chainInter = pubData[key].chainInter;
            //var subType = pubData[key].subType;
            //var sourceFrom = pubData[key].sourceFrom;//more出品
            var uid1 = pubData[key].uid;
            var type1 = pubData[key].type;
            var commentCount = pubData[key].commentCount;//评论人数
            var comments = pubData[key].comments;//评论内容
            time(createTime)
            var dataTime = time(createTime)

            // for (var i = 0; i < pubData[key].likeDto.userLikes.length;i++){
            //   if (uid == pubData[key].likeDto.userLikes[i].uid) {
            //       reday = true
            //      break;
            //   }else{
            //      reday = false
            //   }
            // }


            var obj = {};//创建新对象
            obj.thumb = thumb;
            obj.nickName = nickName;
           // obj.prefeerContent = prefeerContent;
            obj.dataTime = dataTime;
            obj.chainInter = chainInter;
            obj.photos = photos;
            obj.isClick = isClick;
            obj.likeTimes = likeTimes;
            obj.merchantName = merchantName;
           // obj.subType = subType;
            //obj.sourceFrom = sourceFrom;
            obj.type1 = type1;
            obj.pid = pid;
            obj.uid1 = uid1;
            obj.content = content;
            obj.userLikes = userLikes;
            obj.commentCount = commentCount;
            obj.comments = comments;
            pubList.push(obj);
          };
          console.log(pubList)
          if (pubList != null) {

            var oldlist = that.data.totallist;
            for (var i = 0; i < pubList.length; i++) {
              oldlist.push(pubList[i])
            }
          }
         
          that.setData({
            pubList: pubList,
            uid: uid,
            totallist:oldlist
          })

        }
      }
    })


    //订单列表
    //var part = 'refund uncomm unused'
    
    // wx.request({
    //   url: https + 'v2/mpackage/order/list/all' + '?uid=' + uid + '&pn=1&ps=30&access_token=' + access_token,
    //   success: function (res) {
    //     console.log(res.data.data)
    //       var orderData = res.data.data.list
    //       var datalist = [];//创建一个新数组
    //       for (var key in orderData) {
    //         var title = orderData[key].title;
    //         var totalPrice = orderData[key].totalPrice.toFixed(2);//总价
    //         var actualPrice = orderData[key].actualPrice.toFixed(2);//单价
    //         var itemNum = orderData[key].itemNum;//数量
    //         var merchantId = orderData[key].merchantId;
    //         var payTime = orderData[key].payTime;
    //         var pid = orderData[key].pid;
    //         var prdType = orderData[key].prdType;
    //         var orderId = orderData[key].orderId;
    //         var status = orderData[key].status;//状态
          

    //         var obj = {};//创建新对象
    //         obj.title = title;
    //         obj.totalPrice = totalPrice;
    //         obj.actualPrice = actualPrice;
    //         obj.itemNum = itemNum;
    //         obj.merchantId = merchantId;
    //         obj.payTime = payTime;
    //         obj.pid = pid;
    //         obj.prdType = prdType;
    //         obj.status = status;
    //         obj.orderId = orderId;

    //         datalist.push(obj);

    //       } 
    //         console.log(datalist)

    //         that.setData({
    //           datalist: datalist,
    //         })
           
    //   }  
    // })


    //里程
    wx.request({
      url: https + 'v1/user/mycenter/' + uid + '?access_token=' + access_token,
      success: function (res) {
          console.log(res)
          var miledata = res.data.data;
          var cardLevel = miledata.cardLevel;
          var curMiles = miledata.curMiles;//里程
          var regDays = miledata.regDays;
          var cardLimit = miledata.cardLimit;//总里程
          var leng = (curMiles/cardLimit)*w;
          var mLeft = Math.round(leng)+10;
          console.log(mLeft)
          that.setData({
            regDays: regDays,
            curMiles: curMiles,
            cardLevel: cardLevel,
            mLeft: mLeft,
            w:w
          })
      }  
    })


  },


  //遮罩层
  openOpcaty: function () {
    var that = this;
    that.setData({
      isOpacity: true
    })
  },
  colse: function () {
    var that = this;
    that.setData({
      isOpacity: false
    })
  },

  //进入发布动态
  pubstate: function () {
    var that = this;
    wx.navigateTo({
      url: '../dynamics/dynamics'
    })
    that.setData({
      isOpacity: false
    })
  },
  //进入发拼座
  openInter: function () {
    var that = this;
    wx.navigateTo({
      url: '../interaction/interaction'
    })
    that.setData({
      isOpacity: false
    })
  },

  editAddress: function (e) {
    let that = this;
    var access_token = wx.getStorageSync('inToken');
    var uid = wx.getStorageSync('uid');
    var datalist = wx.getStorageSync('datalist');
    console.log(e)
    var orderId = e.currentTarget.dataset.orderid
    var idx = e.currentTarget.dataset.idx
    //触摸时间距离页面打开的毫秒数  
    var touchTime = that.data.touch_end - that.data.touch_start;
    console.log(touchTime);
    //如果按下时间大于350为长按  
    if (touchTime > 350) {
      wx.showModal({
        title: '订单删除',
        content: '确认要删除订单',
        success: function (res) {
          console.log(res)
          if (res.confirm) {
            wx.request({
              url: https + 'v2/mpackage/order/del/' + orderId +'?uid='+uid +'&access_token=' + access_token,
              method: 'POST',
              header: {
                'content-type': 'application/json'
              },
              data: {
                'uid':uid,
                'access_token': access_token

              },
              success: function (res) {
                if(res.data.success){
                  if (res.data.data == 'SUCCESS') {
                      // var newdata = datalist.splice(idx,1)
                      var datalist = that.data.datalist
                      datalist.splice(idx,1)
                      that.setData({
                        datalist: datalist
                      })
                  }
                }
                
              console.log(res)
                
              }  
            })

         
          }
        }
      })
    } else {
      // wx.navigateTo({
        
      // })
    }
  },

  praiseBtn: function (e) {
    console.log(e)
    var that = this;
    var uid = wx.getStorageSync('uid');
    var access_token = wx.getStorageSync('inToken');
    var total = that.data.totallist;
    var thumb = wx.getStorageSync('thumb');
    var nickName = wx.getStorageSync('myUsername');
    var idx1 = e.currentTarget.dataset.idx;
    isInto = false

    //  wx.request({
    //    url: https + 'v1/information/aordeliketime?uid=' + uid + '&fid=' + total[idx1].pid + '&type=' + total[idx1].type1 + '&access_token=' + access_token,
    //    success: function (res) {
    //      console.log(res)
    //    }
    //  })

    if (total[idx1].isClick && total[idx1].userLikes != '') {

      for (var j in total[idx1].userLikes) {
        if (total[idx1].userLikes[j].uid == uid) {
          var n = j;
          total[idx1].userLikes.splice(n, 1)

          total[idx1].userLikes = total[idx1].userLikes
          total[idx1].isClick = false
          total[idx1].likeTimes -= 1
          that.setData({
            totallist: total,
            idx1: idx1,
            isClick: total[idx1].isClick
          })
        }
      }
      try {
        wx.setStorageSync('total', total);
      } catch (e) {
      }

    } else {
      if (total[idx1].userLikes) {
        var o = {}
        o.thumb = thumb;
        o.uid = uid;
        o.nickName = nickName;
        total[idx1].userLikes.push(o)
        total[idx1].isClick = true
        total[idx1].likeTimes += 1
        that.setData({
          totallist: total,
          idx1: idx1,
          isClick: total[idx1].isClick
        })
        try {
          wx.setStorageSync('total', total);
        } catch (e) {
        }
      } else {
        var user = []
        var o = {}
        o.thumb = thumb;
        o.uid = uid;
        o.nickName = nickName;
        user.push(o)
        total[idx1].userLikes = user
        total[idx1].isClick = true
        total[idx1].likeTimes += 1
        console.log(total)
        that.setData({
          totallist: total,
          idx1: idx1,
          isClick: total[idx1].isClick
        })
        try {
          wx.setStorageSync('total', total);
        } catch (e) {
        }
      }

    }


  },

  //按下事件开始  
  mytouchstart: function (e) {
    let that = this;
    that.setData({
      touch_start: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-start')
  },
  //按下事件结束  
  mytouchend: function (e) {
    let that = this;
    that.setData({
      touch_end: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-end')
  },  

  //提示开发中
  develop: function () {
    wx.showModal({
      title: '提示',
      content: '功能正在开发中，敬请期待',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {

        }
      }
    })
  },
  //进入聊天列表
  into_chatlist: function () {
    var that = this;
    // that.setData({
    //   showbox: false,
    //   isinto: true
    // })
    try {
      wx.removeStorageSync('chatMsg1')
    } catch (e) {
    }

    wx.navigateTo({
      url: '../chatlist/chatlist'
    })
    //isinto:true


    wx.setTopBarText({
      text: 'More摩儿-在酒吧发现有趣朋友'
    })

    // wx.setTopBarText({
    //   text: 'More精选酒吧'
    // })
  },
  //返回上页
  backTag: function () {
    wx.navigateBack({
      delta: 2
    })
  },
  //跳转另个小程序
  tiaozhuan: function () {
    wx.navigateToMiniProgram({
      appId: 'wx3abd6a1e615ce065',
      path: '',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'release',
      success(res) {
        // 打开成功
      }
    })
  }

})